package com.example.exp_4;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Register extends AppCompatActivity {
    boolean isPasswordCorrect = false;
    boolean isYanzhengmaCorrect = false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        EditText phonenumber,name,firstpassword,secondpassword,yanzhengma;
        Button btnSubmit,btnRetry;
        RadioGroup radioGroup;
        RadioButton rbMale,rbFemale;
        CheckBox agree;
        Person person = new Person();
        phonenumber = (EditText) findViewById(R.id.phonenumber);
        name = (EditText) findViewById(R.id.name);
        firstpassword = (EditText) findViewById(R.id.firstpassword);
        secondpassword = (EditText) findViewById(R.id.secondnumber);
        yanzhengma = (EditText) findViewById(R.id.yanzhengma);
        btnSubmit = (Button) findViewById(R.id.submit);
        btnRetry = (Button) findViewById(R.id.retry);
        radioGroup = (RadioGroup) findViewById(R.id.sex);
        rbMale = (RadioButton) findViewById(R.id.male);
        rbFemale = (RadioButton) findViewById(R.id.female);
        agree = (CheckBox) findViewById(R.id.agree);

        secondpassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {

                } else {
                    Pattern pattern = Pattern.compile(firstpassword.getText().toString());
                    Matcher matcher = pattern.matcher(secondpassword.getText().toString());
                    boolean isCorrect = matcher.matches();
                    if (isCorrect) {
                        isPasswordCorrect = true;
                    } else {
                        Toast.makeText(getApplicationContext(),"前后密码不一致！",Toast.LENGTH_SHORT).show();
                        isPasswordCorrect = false;
                    }
                }
            }
        });
        btnRetry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phonenumber.setText("");
                name.setText("");
                firstpassword.setText("");
                secondpassword.setText("");
                yanzhengma.setText("");
                radioGroup.clearCheck();
            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (name.getText().toString().equals("")) {
                    Toast.makeText(getApplicationContext(),"请填写姓名",Toast.LENGTH_SHORT).show();
                } else if (phonenumber.getText().toString().equals("")) {
                    Toast.makeText(getApplicationContext(),"请填写手机号",Toast.LENGTH_SHORT).show();
                } else if (!rbMale.isChecked() && !rbFemale.isChecked()) {
                    Toast.makeText(getApplicationContext(),"请选择性别",Toast.LENGTH_SHORT).show();
                } else if (firstpassword.getText().toString().equals("") || secondpassword.getText().toString().equals("")) {
                    Toast.makeText(getApplicationContext(),"请填写密码",Toast.LENGTH_SHORT).show();
                } else if (yanzhengma.getText().toString().equals("")) {
                    Toast.makeText(getApplicationContext(),"请填写验证码",Toast.LENGTH_SHORT).show();
                } else if(yanzhengma.getText().toString().equals("7364")) {
                        isYanzhengmaCorrect = true;
                    if (!agree.isChecked()) {
                        Toast.makeText(getApplicationContext(),"请同意相关协议",Toast.LENGTH_SHORT).show();
                    } else {
                        person.setName(name.getText().toString());
                        person.setNumber(phonenumber.getText().toString());
                        RadioButton radioButton = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());
                        person.setSex(radioButton.getText().toString());
                        person.setPassword(secondpassword.getText().toString());
                        SharedPreferences sharedPreferences = getSharedPreferences("person",MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString("number",person.getNumber());
                        editor.putString("name",person.getName());
                        editor.putString("sex", person.getSex());
                        editor.putString("password", person.getPassword());
                        editor.commit();
                        Toast.makeText(getApplicationContext(),"注册成功",Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(Register.this,MainActivity.class);
                        startActivity(intent);
                    }
                } else {
                    Toast.makeText(getApplicationContext(),"验证码错误",Toast.LENGTH_SHORT).show();
                    isYanzhengmaCorrect = false;
                    yanzhengma.setText("");
                }
            }
        });
    }
}
